﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace SwormTests.SimpleQueries
{
    public class simple1 : TestBase
    {
        public simple1(DbToTest type) : base(type) { }

        class entity
        {
            [Sworm.Column(IsPrimaryKey = true, IsAutoIncrement = false)]
            public long id { get; set; }
            public string name { get; set; }
            public Guid guid { get; set; }
        }

        [Test]
        public void can_list()
        {
            var orig = new entity() { id = 1, guid = Guid.NewGuid(), name = "hello" };
            DB.Insert(orig);

            var item = DB.Query.Simple<entity>().List().Single();
            Assert.AreEqual(1, item.id);
            Assert.AreEqual("hello", item.name);
            Assert.AreEqual(orig.guid, item.guid);
        }

        [Test]
        public void with_where_clause()
        {
            var orig = new entity() { id = 1, guid = Guid.NewGuid(), name = "hello" };
            DB.Insert(orig);

            var item = DB.Query.Simple<entity>()
                .Append("select * from entity where id = @0", 1)
                .List().Single();

            Assert.AreEqual(1, item.id);
            Assert.AreEqual("hello", item.name);
            Assert.AreEqual(orig.guid, item.guid);

            var none = DB.Query.Simple<entity>()
                .Append("select * from entity where id = @0", 2)
                .List();
            Assert.AreEqual(0, none.Count);

            none = DB.Query.Simple<entity>()
                .Append("select * from entity")
                .Where("id = @0", 2)
                .List();
            Assert.AreEqual(0, none.Count);
        }

        [Test]
        public void no_results_returned()
        {
            var orig = new entity() { id = 1, guid = Guid.NewGuid(), name = "hello" };
            DB.Insert(orig);

            var none = DB.Query.Simple<entity>()
                .Append("select * from entity where id = @0", 2)
                .List();
            Assert.AreEqual(0, none.Count);
        }

        [Test]
        public void select_specific_columns()
        {
            var orig = new entity() { id = 1, guid = Guid.NewGuid(), name = "hello" };
            DB.Insert(orig);

            var fromDB = DB.Query.Simple<entity>()
                .Select("id, name")
                .FromDefaultTable()
                .Single();

            Assert.AreEqual(1, fromDB.id);
            Assert.AreEqual("hello", fromDB.name);
            Assert.AreEqual(default(Guid), fromDB.guid);

            fromDB = DB.Query.Simple<entity>()
                .Select(x => x.id, x => x.guid)
                .FromDefaultTable()
                .Single();

            Assert.AreEqual(1, fromDB.id);
            Assert.AreEqual(default(string), fromDB.name);
            Assert.AreEqual(orig.guid, fromDB.guid);
        }
    }
}
